<?php
namespace App\Http\Controllers;

use DB;
use App\Models\Admin_group;
use App\Models\Admin_group_perm;

use Request, Config, Session, Validator, Hash, Res;
class GroupController extends PublicController {    
    public function _initialize(){
    }
    
    public function anyList(){
        $result = [];
        

        $admin_group_list = Admin_group::orderBy('group_id','desc')->paginate(15);
 
        $result['admin_group_list'] = $admin_group_list;
        
        return view('admin.group.list')->with('result', $result);
    }
    
    public function anyAdd(){
        if (!Request::has('flag')) {
            $result = [];
            $perm = Config::get('perm');
            unset($perm['index']);
//            var_dump($perm);exit;
            $result['perm'] = $perm;
            return view('admin.group.add')->with('result', $result);
        }

        $validator = Validator::make(Request::all(), array(
                    'groupname' => 'required|alpha_dash|max:7',
        ));
        if ($validator->fails()) {
            return Res::showMessage('error',$validator->errors(),Session::get('admin.currentRoute'));
        }
        $params=Request::all();        
        DB::beginTransaction();
        if ($admin_group = Admin_group::create($params)) {            
            $permg_param['group_id'] = $admin_group->id;
            if (!empty($params['perm'])) {
                foreach($params['perm'] as $k => $v){
                    $permg_param['perm'] = $v;
                    if (!Admin_group_perm::create($permg_param)) {
                        DB::rollback();
                        return Res::showMessage('error','添加角色失败',Session::get('admin.currentRoute'));
                    }
                }
            }
            DB::commit();
            return redirect('group/list');
        }  else {
            DB::rollback();
            return Res::showMessage('error','添加角色失败',Session::get('admin.currentRoute'));
        }
    }
    
    public function anyEdit(){
        $validator = Validator::make(Request::all(), array(
                    'group_id' => 'required|numeric',
        ));
        if ($validator->fails()) {
            return Res::showMessage('error', $validator->errors(), 'group/list');
        }
        $group_id = Request::get('group_id');
        $admin_group = Admin_group::where('group_id', $group_id)->first();
        if(empty($admin_group)){
            return Res::showMessage('error','参数错误','group/list');
        }
        
        if (!Request::has('flag')) {
            $result = [];
            $result['admin_group'] = $admin_group;
            $able_perms = Admin_group_perm::where('group_id',$group_id)->lists('perm')->toArray();
            $perm = Config::get('perm');
            unset($perm['index']);
//            var_dump($perm);exit;
            $result['able_perm'] = $able_perms;
            $result['perm'] = $perm;
            return view('admin.group.edit')->with('result', $result);
        }

        $validator = Validator::make(Request::all(), array(
                    'groupname' => 'required|alpha_dash|max:7',
        ));
        if ($validator->fails()) {
            return Res::showMessage('error',$validator->errors(),Session::get('admin.currentRoute'));
        }
        $params=Request::all();
        $admin_group->groupname = $params['groupname'];
        DB::beginTransaction();
        if ($admin_group->save()) {
            $permg_param['group_id'] = $group_id;
            //先删除
            Admin_group_perm::where('group_id',$group_id)->delete();
            if (!empty($params['perm'])) {
                foreach($params['perm'] as $k => $v){
                    $permg_param['perm'] = $v;
                    if (!Admin_group_perm::create($permg_param)) {
                        DB::rollback();
                        return Res::showMessage('error','编辑角色失败',Session::get('admin.currentRoute').'?group_id='.$group_id);
                    }
                }
            }
            DB::commit();
            return redirect('group/list');
        }  else {
            DB::rollback();
            return Res::showMessage('error','编辑角色失败',Session::get('admin.currentRoute').'?group_id='.$group_id);
        }
    }
    
    public function anyDel(){
        $validator = Validator::make(Request::all(), array(
                    'group_id' => 'required|numeric',
        ));
        if ($validator->fails()) {
            return Res::showMessage('error', $validator->errors(), 'group/list');
        }
        $group_id = Request::get('group_id');
        Admin_group::where('group_id',$group_id)->delete();
        return redirect('group/list');
    }
}
